package io.renren.controller;


import cn.hutool.core.lang.Validator;
import io.renren.annotation.Login;
import io.renren.annotation.LoginUser;
import io.renren.common.utils.R;
import io.renren.entity.SmsLogEntity;
import io.renren.entity.StudentEntity;
import io.renren.service.SmsLogService;
import io.renren.service.StudentService;
import io.renren.service.TokenService;
import io.renren.util.CharUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录接口
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-23 15:31
 */
@RestController
@RequestMapping("/api/student")
@Api(tags = "登录接口")
public class ApiLoginController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private SmsLogService smsLogService;


    /***
     * 登录
     * 账号可以是手机号或者身份证
     * @param account 账号(手机或身份证)
     * @param password 密码
     * @return
     */
    @PostMapping("login")
    @ApiOperation("登录")
    public R login(@RequestParam("account") String account,
                   @RequestParam("password") String password) {
        Map<String, Object> map = studentService.login(account, password);
        return R.ok(map);
    }

    /***
     * 小程序
     * 登录(手机号码+验证码)
     * @param phone 手机
     * @param smsCode 验证码
     * @return
     */
    @PostMapping("XLogin")
    @ApiOperation("小程序登录")
    public R XLogin(@RequestParam("phone") String phone,
                    @RequestParam("smsCode") String smsCode) {
        Map<String, Object> map = studentService.XLogin(phone, smsCode);
        return R.ok(map);
    }

    /***
     * 退出账号
     * @param student 学生
     * @return
     */
    @Login
    @PostMapping("logout")
    @ApiOperation("退出")
    public R logout(@LoginUser StudentEntity student) {

        return studentService.logout(student);
    }


    /***
     * 发送短信(采用阿里云短信)
     * @param phone 手机号
     * @return
     */
    @RequestMapping("smsCode")
    @ApiOperation("发送短信")
    public R smsCode(String phone) {
        if (!Validator.isMobile(phone)) {
            return R.fail("请输入正确的手机号码");
        }
        // 一分钟之内不能重复发送短信
        SmsLogEntity smsLog = smsLogService.querySmsCodeByPhone(phone);
        if (null != smsLog && (System.currentTimeMillis() / 1000 - smsLog.getLogDate()) < 5 * 60) {
            return R.fail("短信已发送");
        }
        //生成验证码
        String smsCode = CharUtil.getRandomNum(4);
        String msgContent = "您的验证码是：" + smsCode + "，请在页面中提交验证码完成验证。";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("code", smsCode);
        // Boolean sendCode = ALiSmsUtils.sendCode("春芽健康科技", "SMS_143810054", paramMap, phone);
        //         if (!sendCode) {
        //            return toResponsFail("短信发送失败");
        //                        }
        smsLog = new SmsLogEntity();
        //生成时间戳
        smsLog.setLogDate(System.currentTimeMillis() / 1000);
        smsLog.setPhone(phone);
        smsLog.setSmsText(msgContent);
        smsLog.setSmsCode(smsCode);
        smsLogService.insert(smsLog);
        return R.ok(smsCode);
    }


}
